package com.tanhua.server.service;

import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.Vioce;
import com.tanhua.server.vo.PeachBlossom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.data.mongodb.core.MongoTemplate;
//import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.Random;

@Slf4j
@Service
public class PeachBlossomService {

    @Autowired
    private UserInfoService userInfoService;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Autowired
    private MongoTemplate mongoTemplate;

    public PeachBlossom queryPeachBlossom() {

        //无需校验，直接获取
        User user = UserThreadLocal.get();

        //获取userinfo中的信息
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserIdPeachBlossom(user.getId());
        if (null == userInfo) {
            return null;
        }

        /*把数据封装到PeachBlossom中返回给前端*/

        PeachBlossom peachBlossom = new PeachBlossom();
        //设置头像
        peachBlossom.setAvatar(userInfo.getLogo());
        //设置昵称
        peachBlossom.setNickname(userInfo.getNickName());
        //设置性别
        peachBlossom.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        //设置年龄
        peachBlossom.setAge(userInfo.getAge());

        //设置剩余次数
        Random random = new Random();
        peachBlossom.setRemainingTimes(random.nextInt(10));

        Query query = Query.query(Criteria.where("userId").is(userInfo.getId()));
        Vioce pb = mongoTemplate.findOne(query, Vioce.class);
        String soundUrl = pb.getSoundUrl();
        peachBlossom.setSoundUrl(soundUrl);

        return peachBlossom;
    }

}
